+function($){
	
	var Load = function(element,option){
		this.$element = $(element)
		this.options = $.extend({}, Load.DEFAULT.imgsrcs, option);
	}
	
	Load.DEFAULT = {
		imgsrcs : ['http://static.bootcss.com/www/assets/img/headroom.png','http://static.bootcss.com/www/assets/img/headroom.png','http://static.bootcss.com/www/assets/img/headroom.png','http://static.bootcss.com/www/assets/img/headroom.png','http://static.bootcss.com/www/assets/img/headroom.png']
	}
	
	
	Load.prototype.getImagesSrc = function(){
		var srcs = []
		var options = this.options
		var $images = $(document).find('img')
		var defaultImgsrcs = options.imgsrcs
		var allsrcs
		$images.each(function(i,image){
			var $image = $(image)
			var src = $image.attr('src')
			srcs.push(src)
		})
		
		allsrcs = srcs.concat(defaultImgsrcs)
		return allsrcs
	}
	
	
	
	Load.prototype.asyncLoadImage = function(){
		var dfd = new $.Deferred()
		var srcs = this.getImagesSrc()
		var count = 0
		var self = this
		var len = srcs.length
		var loadlen = Math.ceil(len*0.8)
		var loadState = 'complete'
		var imageloadState
		$.each(srcs, function(i,src) {
			imageloadState = self.imgload(src,count,loadlen)
			if(loadState == imageloadState){
				dfd.resolve('ok')
			}else{
				count++
			}
		});
		return dfd.promise()
	}
	
	Load.prototype.imgload = function(src,count,len){
		var image = new Image()
		image.onload  = function(){
			image.onload = null
			image = null
		}
		image.onerror = function(){
			image.onerror = null
			image = null
		}
		image.src = src
		if(count >= len){
			return 'complete'
		}
	}
	
	Load.prototype.when = function(){
		var self = this
		$.when(this.asyncLoadImage()).then(function(status){
        	console.log( status+', 加载图片' );
    	})
	}
	
	
	function Plugin(option){
		return this.each(function () {
	      var $this   = $(this)
	      var data    = $this.data('mg.Load')
	      var options = $.extend({}, Load.DEFAULT, $this.data(), typeof option == 'object' && option)
	      if (!data && options.toggle && /show|hide/.test(option)) options.toggle = false
	      if (!data) $this.data('mg.Load', (data = new Load(this, options)))
	      if (typeof option == 'string') data[option]()
	    })
	}
	
	$.fn.load = Plugin
	$.fn.load.Constructor = Load
	
	
}(jQuery)
